﻿using CaseManagerLibrary.Socket;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace PSDServer.Protocols
{
    public class ServerMessageProcessor
    {
        public static Dictionary<string, ServerMessageProcessor> processorSet = new Dictionary<string, ServerMessageProcessor>();

        public MessageBase Message { get; set; }
        public static ServerMessageProcessor Parse(MessageBase message)
        {
            ServerMessageProcessor msgProcess = null;

            try
            {
                string processer = "PSDServer.Protocols." + message.Name + "Processor";

                if (processorSet.ContainsKey(processer))
                {
                    msgProcess = processorSet[processer];
                }

                if (msgProcess == null)
                {
                    System.Reflection.Assembly assm = System.Reflection.Assembly.Load("PSDServer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
                    Type processerType = assm.GetType(processer);
                    msgProcess = (ServerMessageProcessor)Activator.CreateInstance(processerType);
                    processorSet.Add(processer, msgProcess);
                }

                msgProcess.Message = message;
            }
            catch (Exception ex)
            { }

            return msgProcess;
        }

        public virtual void Run(PSDServerContext context, TcpClientState state,  params object[] args)
        {
        }
    }
}
